Python calculating

Table of Contents

1. Get started

Create a folder in which to store your work for this assignment.

  • If you are working on your own computer, it’s up to you where to put the folder. Your desktop is likely as good a place as any. Make a folder titled pythonlab2.
  • If you are working in the labs in Olin, make sure to first mount the COURSES folder, so that you won’t lose your code when you log out. Once you’ve done so, open up Finder, then navigate to your personal student work folder. You can then make a pythonlab2 folder within there.
  • Once you’ve done so, you should then open up your new folder in VS Code. To do so, start up VS Code, then drag your folder onto the VS Code window. This should open up the folder within VS Code. If you are asked, click that you trust the authors.

2. Metric conversion

Create a file named inchescm.py in your folder. Write a program to prompts the user for a number of feet, and then a number of inches. The program should convert this amount to centimeters. Your output should look exactly like the following, but should also work for other inputs.

(A foot is 12 inches, and an inch is 2.54 centimeters.)

This program will convert English units to metric.
Enter number of feet: 5
Enter number of inches: 11
5 ft 11 in = 180.34 cm

3. Receipt

Create a file named receipt.py in your folder, and then copy into it the following code:

print("Meal total:")          # Compute total
print(38 + 40 + 30)           # owed, assuming
print("Tax:")                 # 8% tax and
print((38 + 40 + 30) * .08)   # 15% tip
print("Tip:")
print((38 + 40 + 30) * .15)
print("Final total:")
print(38 + 40 + 30 + (38 + 40 + 30) * .08 + (38 + 40 + 30) * .15)

Modify the program to use variables so that the same calculations don’t have to be repeated. It should still produce the same output.

4. Receipt2

Create a file named receipt2.py in your folder, and then copy into it the following code:

# Calculate total owed, assuming 8% tax / 15% tip
subtotal = 38 + 40 + 30
tax = subtotal * .08
tip = subtotal * .15
total = subtotal + tax + tip

print("Meal total:", subtotal)
print("Tax:", tax)
print("Tip:", tip)
print("Final total:", total)

Modify the program so that instead of having fixed values, it prompts the user to enter the cost and then computes the rest. For example:

What was the meal cost? 50
Meal total: 50
Tax: 4.0
Tip: 7.5
Final total: 61.5
What was the meal cost? 125
Meal total: 125
Tax: 10.0
Tip: 18.75
Final total: 153.75

5. Sum of square roots

Create a file called sum.py in your folder. Write a program that prompts the user for a positive integer, and then prints the sum of the square roots of the integers less than or equal to that number. Round the answer off to three decimal places. For example, if the user enters 5, the program will output the results of

\[\sqrt 0 + \sqrt 1 + \sqrt 2 + \sqrt 3 + \sqrt 4 + \sqrt 5 \]

which is 8.382. Here is what the program should look like when running:

What is the positive integer? 5
The sum is 8.382

(Yep, I know that \(\sqrt0\) is \(0\). I’m just writing that as the first term in the sum to be more consistent with how your Python program may go about it.)

6. Triangle

Create a file called triangle.py in your folder. Write a program that prompts the user for a positive integer, and then prints out a triangle like the one below:

How many dashes should appear on the bottom row? 5
 *
- *
-- *
--- *
---- *
----- *

Here’s a trick that is super helpful: you can tell Python to take a single character and repeat it. For example, if I tell Python to

print("a"*4)

… then here’s the output I’ll get:

aaaa

7. Repeating triangles

Create a file called repeating_triangles.py in your folder. Write a program that prompts the user for two positive integers, and then prints out a pattern like the one below:

How many rows are there in each group? 4
How many groups should there be? 2
-
--
---
----
-
--
---
----

The first number indicates how many rows are in each group, and each group consists of rows of increasing number of dashes, ranging from one dash up until the number of rows in the group. The second number indicates how many groups there should be. Here’s another example:

How many rows are there in each group? 2
How many groups should there be? 4
-
--
-
--
-
--
-
--

You should do this just with single definite for loop. (If you happen to be aware of nested loops, which are coming later in the course, don’t use them.) The trick here is to use the remainder operator (%).

8. Grading

To receive a grade of “Meets expectations” for this assignment, you need to successfully complete all of the tasks above, except for the two triangle tasks. If you complete everything successfully including the triangle tasks, that will earn a grade of “Exemplary.” For the programs that require you to input values, your programs should work for any reasonable value we enter, not just the examples that I’ve shown. Your code also must not be excessively complex or convoluted, or use more advanced programming tricks that we haven’t discussed yet. (In general, I’m happy for you to go ahead and learn more; the reason I don’t want you using advanced techniques for this assignment is that they likely make the problem simpler than it is intended to be.)

9. Turn in your programs

Unlike the in-class CodeCheck labs, these are not being autograded or connected directly to your Moodle account. Instead, you should submit this code to Moodle by uploading a single zip file. Don’t upload your Python programs one-by-one; instead, upload a single zip file that contains all of them. Do this by zipping (compressing) the entire folder, not each individual file. Here are instructions on how to do this on a Mac, and on Windows.

Once your zip file has been created, upload it to this Moodle assignment.

10. How get help

Remember that you have many sources of help! See the “How to get help” section of our Moodle page, which is in the Course Blocks at the top of the page.


Some of the exercises in this assignment are variations or inspirations from the book “Building Java Programs: A Back to Basics Approach or from supplemental exercises created by the authors of that book.

Author: Dave Musicant